package com.orangelife.service.statistics.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.statistics.IReportingService;
import com.orangelife.webservice.service.statistics.entity.StatisticsDto;

@Service("reportingService")
public class ReportingServiceImpl extends MybaitsGenericDaoSubLogic implements
		IReportingService {

	@Override
	public List<Map<String, Object>> clearOwe(StatisticsDto sta)
			throws AppBizException {

		if (sta.getIsPage() == 1) {

			PageHelper.startPage(Integer.valueOf(sta.getPageIndex()),
					Integer.valueOf(sta.getPageSize()));

		}

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("commID", sta.getCommID());

		if (sta.getFeeTypeIDs() != null && sta.getFeeTypeIDs().length > 0) {

			param.put("feeTypeIDs", sta.getFeeTypeIDs());

		}

		if (sta.getIsall() != null) {

			param.put("isall", sta.getIsall());

		}

		param.put("apartmentType", sta.getApartmentType());
		param.put("feeTypeID", sta.getFeeTypeID());
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());
		List<Map<String, Object>> l = this.getSqlSession().selectList(
				"ReportingMapper.clearOwe", param);

		// TODO Auto-generated method stub
		return l;
	}

	@Override
	public List<Map<String, Object>> fullIncome(StatisticsDto sta)
			throws AppBizException {
		if (sta.getIsPage() == 1) {

			PageHelper.startPage(Integer.valueOf(sta.getPageIndex()),
					Integer.valueOf(sta.getPageSize()));

		}

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("commID", sta.getCommID());

		if (sta.getApartmentTypes() != null
				&& sta.getApartmentTypes().length > 0) {

			param.put("apartmentTypes", sta.getApartmentTypes());

		}

		if (sta.getFeeTypeIDs() != null && sta.getFeeTypeIDs().length > 0) {

			param.put("feeTypeIDs", sta.getFeeTypeIDs());

		}

		if (sta.getIsall() != null) {

			param.put("isall", sta.getIsall());

		}
		param.put("apartmentType", sta.getApartmentType());

		param.put("feeTypeID", sta.getFeeTypeID());
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());
		List<Map<String, Object>> l = this.getSqlSession().selectList(
				"ReportingMapper.fullIncome", param);

		// TODO Auto-generated method stub
		return l;
	}

	@Override
	public List<Map<String, Object>> payRate(StatisticsDto sta)
			throws AppBizException {
		if (sta.getIsPage() == 1) {

			PageHelper.startPage(Integer.valueOf(sta.getPageIndex()),
					Integer.valueOf(sta.getPageSize()));

		}

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("commID", sta.getCommID());

		if (sta.getApartmentTypes() != null
				&& sta.getApartmentTypes().length > 0) {

			param.put("apartmentTypes", sta.getApartmentTypes());

		}

		if (sta.getFeeTypeIDs() != null && sta.getFeeTypeIDs().length > 0) {

			param.put("feeTypeIDs", sta.getFeeTypeIDs());

		}
		if (sta.getIsall() != null) {

			param.put("isall", sta.getIsall());

		}

		if (sta.getIsExport() == 1) {
			String[] Mons = { "01", "02", "03", "04", "05", "06", "07", "08",
					"09", "10", "11", "12" };
			List<Map<String, Object>> m = new ArrayList<Map<String, Object>>();

			String sql2 = "";
			for (String string : Mons) {
				sql2 += "SUM( CASE WHEN t.fee_date = '"
						+ sta.getEndTime()
						+ "-"
						+ string
						+ "' THEN t.amount ELSE 0 END ) M"
						+ string
						+ "_Money, sum( CASE WHEN t.fee_date = '"
						+ sta.getEndTime()
						+ "-"
						+ string
						+ "' AND t. STATUS = 1 AND DATE_FORMAT(t.paytime, '%Y') = '"
						+ sta.getEndTime() + "' THEN t.amount ELSE 0 END ) M"
						+ string + "_PayMoney,";

			}

			param.put("isExport", "1");

			param.put("sql2", sql2);
		}

		param.put("apartmentType", sta.getApartmentType());

		param.put("feeTypeID", sta.getFeeTypeID());
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());
		List<Map<String, Object>> l = this.getSqlSession().selectList(
				"ReportingMapper.payRate", param);

		// TODO Auto-generated method stub
		return l;
	}

	@Override
	public List<Map<String, Object>> payRateByComm(Map<String, Object> map)
			throws AppBizException {
		String[] Mons = { "01", "02", "03", "04", "05", "06", "07", "08", "09",
				"10", "11", "12" };
		List<Map<String, Object>> m = new ArrayList<Map<String, Object>>();

		String sql2 = "";
		for (String string : Mons) {
			sql2 += "SUM( CASE WHEN t.STATUS = 1 and t.fee_date = '"
					+ map.get("time") + "-" + string
					+ "' THEN t.amount ELSE 0 END ) M" + string
					+ "_thisClear, sum( CASE WHEN t.fee_date = '"
					+ map.get("time") + "-" + string
					+ "' AND t.STATUS = 0 THEN t.amount ELSE 0 END ) M"
					+ string + "_noClear,sum( CASE WHEN t.fee_date = '"
					+ map.get("time") + "-" + string
					+ "' THEN t.amount ELSE 0 END ) M" + string + "_amount,";

		}

		map.put("isExport", "1");

		map.put("sql2", sql2);
		List<Map<String, Object>> l = this.getSqlSession().selectList(
				"ReportingMapper.payRateByComm", map);

		return l;
	}
	
	@Override
	public List<Map<String, Object>> payRateYearByComm(Map<String, Object> map)
			throws AppBizException {
		List<Map<String, Object>> l = this.getSqlSession().selectList(
				"ReportingMapper.payRateYearByComm", map);

		return l;
	}

	@Override
	public List<Map<String, Object>> payRateDetail(StatisticsDto sta)
			throws AppBizException {
		if (sta.getIsPage() == 1) {

			PageHelper.startPage(Integer.valueOf(sta.getPageIndex()),
					Integer.valueOf(sta.getPageSize()));

		}

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("commID", sta.getCommID());

		String[] Mons = { "01", "02", "03", "04", "05", "06", "07", "08", "09",
				"10", "11", "12" };
		List<Map<String, Object>> m = new ArrayList<Map<String, Object>>();

		String sql2 = "";
		for (String string : Mons) {
			sql2 += "SUM( CASE WHEN t.fee_date = '"
					+ sta.getEndTime()
					+ "-"
					+ string
					+ "' THEN t.amount ELSE 0 END ) M"
					+ string
					+ "_Money, sum( CASE WHEN t.fee_date = '"
					+ sta.getEndTime()
					+ "-"
					+ string
					+ "' AND t. STATUS = 1 AND DATE_FORMAT(t.paytime, '%Y') = '"
					+ sta.getEndTime() + "' THEN t.amount ELSE 0 END ) M"
					+ string + "_PayMoney,";

		}

		param.put("isExport", "1");

		param.put("sql2", sql2);

		param.put("apartmentID", sta.getApartmentID());
		param.put("apartmentType", sta.getApartmentType());
		param.put("feeTypeID", sta.getFeeTypeID());
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());
		List<Map<String, Object>> l = this.getSqlSession().selectList(
				"ReportingMapper.payRateDetail", param);

		// TODO Auto-generated method stub
		return l;
	}

	@Override
	public List<Map<String, Object>> getOwerOweFeeInfo(StatisticsDto sta)
			throws AppBizException {

		Map<String, Object> param = new HashMap<String, Object>();

		param.put("commID", sta.getCommID());

		param.put("commIDs", sta.getCommIDs());
		param.put("startTime", sta.getStartTime());
		param.put("endTime", sta.getEndTime());
		param.put("level", sta.getLevel());
		
		param.put("ownerName", sta.getOwnerName());
		param.put("isall", sta.getIsall());
		param.put("status", "0");
	  	param.put("startAging", sta.getStartAging());
    	param.put("st", sta.getSt());
    	param.put("endAging", sta.getEndAging());
    	param.put("et", sta.getEt());
		if (sta.getCodes() != null && sta.getCodes().length > 0) {

			param.put("codes", sta.getCodes());

		}
		if (sta.getBulidingIDs() != null && sta.getBulidingIDs().length > 0) {

			param.put("bulidingIDs", sta.getBulidingIDs());

		}

		if (sta.getApartmentTypes() != null
				&& sta.getApartmentTypes().length > 0) {

			param.put("apartmentTypes", sta.getApartmentTypes());

		}

//		int pageSize = sta.getPageSize();
//		PageHelper.startPage(Integer.valueOf(sta.getPageIndex()),
//				Integer.valueOf(pageSize));
//		
		List<Map<String, Object>> res=null;
		if("4".equals(sta.getIsall())){
			
			res= this.getSqlSession().selectList(
					"ReportingMapper.getOwerOweFeeInfo2", param);
			
		}else{
			
res = this.getSqlSession().selectList(
					"ReportingMapper.getOwerOweFeeInfo", param);
		}

		return res;

	}

	@Override
	public List<Map<String, Object>> fullIncomeByComm(Map<String, Object> map)
			throws AppBizException {
		List<Map<String, Object>> l = this.getSqlSession().selectList(
				"ReportingMapper.fullIncomeByComm", map);

		return l;
	}

	@Override
	public List<Map<String, Object>> getAllPropFeeAccountInfo(
			Map<String, Object> map) throws AppBizException {
		List<Map<String, Object>> l = this.getSqlSession().selectList(
				"ReportingMapper.getAllPropFeeAccountInfo", map);

		return l;
	}

	@Override
	public List<Map<String, Object>> clearOweByComm(Map<String, Object> map)
			throws AppBizException {
		List<Map<String, Object>> l = this.getSqlSession().selectList(
				"ReportingMapper.clearOweByComm", map);

		return l;
	}

	@Override
	public List<Map<String, Object>> agingByComm(Map<String, Object> map)
			throws AppBizException {
		List<Map<String, Object>> l = this.getSqlSession().selectList(
				"ReportingMapper.agingByComm", map);

		return l;
	}

	

}
